<template>
  <el-dialog title="退货单查询" :close-on-click-modal="false" :visible.sync="visible" fullscreen append-to-body center> 
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <div class="text item">
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="单据编号">
          <el-input size="mini" v-model="dataForm.returnBillId"></el-input>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" readonly></el-input>
        </el-form-item>
        <el-form-item label="结算日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.settlementTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.settlementTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="商品代码">
          <el-input v-model="dataForm.goodsCode"></el-input>
        </el-form-item>
        <el-form-item label="单据日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" plain @click="searchHandle">查询</el-button>
          <el-button icon="el-icon-search" type="info" plain @click="clearSearchForm">清除</el-button>
        </el-form-item>
      </el-form>
    </div>

      <el-row style="margin-bottom: 3px;">
        <el-button type="primary" @click="choiceAndClose">选择并关闭</el-button>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50"></el-table-column>
          <el-table-column prop="linkBillId" label="单据编号" width="188"></el-table-column>
          <el-table-column prop="settlementTime" label="结算日期"></el-table-column>      
          <el-table-column prop="settlementUsername" label="结算人"></el-table-column>
          <el-table-column prop="allWriteOffAmount" label="单据金额"></el-table-column>
          <el-table-column prop="doneWriteOffAmount" label="已核销金额"></el-table-column>
          <el-table-column prop="notWriteOffAmount" label="未核销金额"></el-table-column>
        </el-table>
      </div>
      <div class="avue-crud__pagination" style="margin-bottom: 25px;">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
      </div>
      <!-- 弹窗，查找制造商 -->
      <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
  
  </el-dialog>
</template>

<script>
  import { filterParams } from "@/util/tools"
  import { returnSettlementFetchList } from "@/api/eims/return-management"
  import { getPinAndPout } from "@/api/eims/payslip"
  import { mapGetters } from "vuex"
  import { getCtgDataList } from "@/api/eims/check"
	import SupplySearch from '@/components/business/SearchSupply'
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          supplyName: ''
        },
        dataList: [],
        settlementStatusOptions: [
          { label: '已审核', value: 1 },
          { label: '未审核', value: 0 }
        ],
        ctgOptions: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        supplyVisible: false,
      };
    },
    components: {
      SupplySearch
    },
    created() {
      this.getGoodsCtgList();
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    methods: {
      init(data) {
        this.visible = true
        this.dataForm.supplyName = data.supplyName
        this.dataForm.supplyId = data.supplyId
        this.dataForm.settlementStatus = '1'
      },
      //	查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'supplyId' || key != 'supplyName'){
            this.dataForm[key] = "";
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        let params2 = filterParams(this.dataForm);
        this.dataListLoading = true;
        getPinAndPout(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      settlementStatusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '1':
            return '已审核'
            break
          case '0':
            return '未审核'
            break
        }
      },
      //获取制造商列表
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init()
        })
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      //获取商品类别
      getGoodsCtgList() {
        getCtgDataList().then(response => {
          if(response.data.code == '0'){
            let list = response.data.data.records
            let options = []
            list.forEach(item => {
              options.push({
                value: item.id,
                name: item.name
              })
            })
            this.ctgOptions = options
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  多选表格改变事件
      handleSelectionChange(val) {
        this.selectionList = val
      },
      //  选择部门并关闭弹窗
      choiceAndClose() {
        this.visible = false
        this.$emit('refreshData',this.selectionList)
      }
    }
  };
</script>
